# coding:utf-8
# -*- coding: utf-8 -*-
# @Time : 2020/9/3 11:58
# @Author: 王鹏-wangpeng99
# @File : main.py

"""
调用链模拟生成工具，用于生成符合要求的调用链
"""
import time, json, os, pathlib
from config.config import SysConfig
from service import ChanLogMakerV1
from service.ChanMaker import ChanMaker
from multiprocessing import Process
from utils import logutils
logger = logutils.getlogger(__file__)


def mkchanjson():
    jsonfile = pathlib.Path('chan.json')
    if jsonfile.exists():
        return jsonfile
    else:
        chanmker = ChanMaker(vertical=SysConfig.vcnt, horizon=SysConfig.hcnt, chancnt=SysConfig.chancnt)
        chandict = chanmker.mkchan()
        with open('chan.json', 'w', encoding='utf-8') as json_file:
            json.dump(chandict, json_file, ensure_ascii=False)


def main():
    mkchanjson()
    with open('chan.json', 'r', encoding='utf-8') as json_file:
        chandict = json.load(json_file)
        print(chandict)
        processlst = []
        for i in range(SysConfig.processcnt):
            p = Process(target=ChanLogMakerV1.mkAopLog, args=(chandict,))
            processlst.append(p)
        for p in processlst:
            p.start()
            time.sleep(0.1)
        for p in processlst:
            p.join()
    print("ok")


def test():
    mkchanjson()
    with open('chan.json', 'r', encoding='utf-8') as json_file:
        chandict = json.load(json_file)
        print(chandict)
        processlst = []
        for i in range(SysConfig.processcnt):
            p = Process(target=ChanLogMakerV1.mkAopLog, args=(chandict,))
            processlst.append(p)
        for p in processlst:
            p.start()
            time.sleep(0.1)
        for p in processlst:
            p.join()
    print("ok")


if __name__ == '__main__':
    main()
    # test()
